//
//  TSUserInfoViewController.m
//  chart
//
//  Created by xu on 15/8/22.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSUserInfoViewController.h"
#import "TSUserInfoEditViewController.h"
#import "TSPasswordEditViewController.h"
#import "AppDelegate.h"
#import "TSLoginCheckBox.h"
#define SEARCH_LABEL_WIDTH  50.f
#define SEARCH_LABEL_HEIGHT 35.f
#define SEARCH_ROW_GAP 10.f

@interface TSUserInfoViewController ()

@end

@implementation TSUserInfoViewController{
    UILabel *titleLabel;
    UILabel *workIdLabel;
    UILabel *sectionLabel;
    UILabel *phoneLabel;
    
    UIView *titleBack;
    UIView *workIdBack;
    UIView *sectionBack;
    UIView *phoneBack;
    TSLoginCheckBox * rememberCheckBox;
    TSLoginCheckBox * autoLoginCheckBox;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [super leftBarButtonWithTitle:nil barNorImage:nil barHltImage:nil action:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUserInfo:) name:TSNotification_EditUserInfoSucceeded object:nil];
       [self setTitle:@"个人信息"];
    [self addTitleRow];
    
    [self addWorkIdRow];
    
    [self addSctionRow];
    
    [self addPhoneRow];
    
    [self addCheckBox];
    
    [self addEditInfoBtn];
    
    [self addEditPwdBtn];

    [self addLogoutBtn];
}

-(void)addCheckBox{
    rememberCheckBox = [[TSLoginCheckBox alloc] init];
    rememberCheckBox.type = LoginCheckBox_Remember;
    [rememberCheckBox initUI];
    [rememberCheckBox setTextColor:[UIColor blackColor]];
    [self.view addSubview:rememberCheckBox];
    
    autoLoginCheckBox = [[TSLoginCheckBox alloc] init];
    autoLoginCheckBox.type = LoginCheckBox_AutoLogin;
    [autoLoginCheckBox initUI];
    [autoLoginCheckBox setTextColor:[UIColor blackColor]];
    [self.view addSubview:autoLoginCheckBox];
    
    
    rememberCheckBox.frame =CGRectMake(MARGIN_LEFT, CGRectGetMaxY(phoneBack.frame)+SEARCH_ROW_GAP, 50, 10);
    
    autoLoginCheckBox.frame =CGRectMake(currentDeviceWidth-MARGIN_LEFT-55, CGRectGetMaxY(phoneBack.frame)+SEARCH_ROW_GAP,50, 10);
}

- (void)refreshUserInfo:(NSNotification *)noti
{
    titleLabel.text = [TSUserModel sharedInstance].name;
    workIdLabel.text = [TSUserModel sharedInstance].workId;
    phoneLabel.text = [TSUserModel sharedInstance].phone;
}

- (UILabel *)createLabel:(CGRect)frame Title:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:title];
    [label setFont:FONT(12.f)];
    
    return label;
}

- (void)addLine:(CGRect)frame
{
    UIView *line = [[UIView alloc] initWithFrame:frame];
    [line setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:line];
}

- (void)addTitleRow
{
     titleBack = [self createTextBackView:MARGIN_TOP];
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, 0, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"姓名:"];
    [titleBack addSubview:label];
    
    
    titleLabel = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, 0, currentDeviceWidth-2*MARGIN_LEFT-SEARCH_LABEL_WIDTH-10.f, SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].name];
 
    [titleBack addSubview:titleLabel];
    [self.view addSubview:titleBack];
    
}

-(UIView *)createTextBackView:(CGFloat)floatY{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, floatY, currentDeviceWidth, SEARCH_LABEL_HEIGHT)];
    [view setBackgroundColor:[UIColor whiteColor]];
    return view;
}

- (void)addWorkIdRow
{
    workIdBack = [self createTextBackView:CGRectGetMaxY(titleBack.frame)+SEARCH_ROW_GAP];
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT,0, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"工号:"];
    [workIdBack addSubview:label];
    
    workIdLabel = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f,0, CGRectGetWidth(titleBack.frame), SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].workId];
    [workIdBack addSubview:workIdLabel];
    [self.view addSubview:workIdBack];
   }

- (void)addSctionRow
{
    sectionBack = [self createTextBackView:CGRectGetMaxY(workIdBack.frame)+SEARCH_ROW_GAP];
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, 0, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"部门:"];
    [sectionBack addSubview:label];
    
    sectionLabel = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, 0, CGRectGetWidth(workIdBack.frame), SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].sectionName];
    [sectionBack addSubview:sectionLabel];
    
    [self.view addSubview:sectionBack];
    
}

- (void)addPhoneRow
{
    phoneBack = [self createTextBackView:CGRectGetMaxY(sectionBack.frame)+SEARCH_ROW_GAP];
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, 0, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"电话:"];
    [phoneBack addSubview:label];
    
    phoneLabel = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, 0, CGRectGetWidth(sectionBack.frame), SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].phone];
    [phoneBack addSubview:phoneLabel];
    [self.view addSubview:phoneBack];
}


/* 修改个人信息 */
- (void)addEditInfoBtn
{
    UIButton *btn = [self createBtn:CGRectMake(0, CGRectGetMaxY(rememberCheckBox.frame)+SEARCH_ROW_GAP, currentDeviceWidth, SEARCH_LABEL_HEIGHT) Title:@"修改信息"];
    [self.view addSubview:btn];
}

/* 修改密码 */
- (void)addEditPwdBtn
{
    UIButton *btn = [self createBtn:CGRectMake(0, CGRectGetMaxY(rememberCheckBox.frame)+2*SEARCH_ROW_GAP+SEARCH_LABEL_HEIGHT, currentDeviceWidth, SEARCH_LABEL_HEIGHT) Title:@"修改密码"];
       [self.view addSubview:btn];
}

/* 退出登录 */
- (void)addLogoutBtn
{
    UIButton *btn = [self createBtn:CGRectMake(0, CGRectGetMaxY(rememberCheckBox.frame)+3*SEARCH_ROW_GAP+2*SEARCH_LABEL_HEIGHT, currentDeviceWidth, SEARCH_LABEL_HEIGHT) Title:@"退出登录"];
    
    [self.view addSubview:btn];
}

- (UIButton *)createBtn:(CGRect)frame Title:(NSString *)title
{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = FONT(12);
    [btn setBackgroundColor:[UIColor whiteColor]];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    return btn;
}

- (void)onBtnClick:(UIButton *)sender
{
    if ([sender.titleLabel.text isEqualToString:@"修改信息"]) {
        [self showEditInfoViewController];
    }
    else if ([sender.titleLabel.text isEqualToString:@"修改密码"]){
        [self showEditPwdViewController];
    }
    else if ([sender.titleLabel.text isEqualToString:@"退出登录"]){
        [self exitApplication];
    }
}
- (void)exitApplication {
    
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
    //exit(0);
    
}


- (void)showEditInfoViewController
{
    TSUserInfoEditViewController *edit_vc = [[TSUserInfoEditViewController alloc] init];
    [self.navigationController pushViewController:edit_vc animated:YES];
}

- (void)showEditPwdViewController
{
    TSPasswordEditViewController *edit_vc = [[TSPasswordEditViewController alloc] init];
    [self.navigationController pushViewController:edit_vc animated:YES];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        WS(weakSelf);
        [[TSUserModel sharedInstance] logout:^(BOOL isSucceeded, NSError *error) {
            if (!weakSelf) {
                return;
            }
            if (isSucceeded) {
                [weakSelf.navigationController dismissViewControllerAnimated:YES completion:^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_Logout object:nil];
                }];
            }
        }];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
